package com.chaopei.mynet;

import java.io.*;
import java.net.Socket;

/**
 * @Author Corday 天涯
 * @Date 2022/3/15 15:26
 * 客户端程序
 */
public class SocketNet {
    public static void main(String[] args) {
        speak();
    }//main

    public static void speak() {
//        声明Socket套接字对象
        Socket sc = null;
//        声明输入输出流对象
        InputStream in = null;
        OutputStream out = null;
        PrintWriter pw = null;
        BufferedReader br = null;
        try {
            //创建对象并传参(本机ip,服务端的端口号)
            sc = new Socket("127.0.0.1", 9081);
            //接收服务端传回的消息
            in = sc.getInputStream();
            out = sc.getOutputStream();
            pw = new PrintWriter(out,true);
            pw.println("我是Client,在吗?");
            br = new BufferedReader(new InputStreamReader(in));
            String msg = br.readLine();
            System.out.println("Client拿到:"+msg);
            pw.close();
            //关闭流
            in.close();
            out.close();
            sc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
